<template>
  <div class="mt-user-supplies">
    <div class="mt-us-tips">
      提示，刷新会让你的信息更靠前。
    </div>
    <tab>
      <tab-item
      v-for="(tab, index) in tabs"
      :key="index"
      :selected="active === index" 
      @on-item-click="changeActiveTab(index)">{{tab}}</tab-item>
    </tab>
    <swiper class="mt-us-content" 
    v-model="active" 
    :show-dots="false"
    height="100%">
      <swiper-item>
        <list v-for="(sale,index) in saleList"
        :data="sale"
        :key="index"
        :type="0"
        @refresh="getList"></list> 
        <nodata v-if="saleList.length === 0"></nodata>
      </swiper-item>
      <swiper-item>
        <list v-for="(unshelvele,index) in unshelveList"
        :data="unshelvele"
        :key="index"
        :type="1"
        @refresh="getList"></list> 
        <nodata v-if="unshelveList.length === 0"></nodata>
      </swiper-item>
      <swiper-item>
        <list v-for="(refuse,index) in refuseList"
        :data="refuse"
        :key="index"
        :type="2"
        :refuseInfo="refuseDesc[refuse.id][0]"
        @refresh="getList"></list>
        <nodata v-if="refuseList.length === 0"></nodata>
      </swiper-item>
    </swiper>
    <loading :show="loading"></loading>
  </div>
</template>

<script>
import { Tab, TabItem, Loading, Swiper, SwiperItem } from "vux"
import List from "@/components/List/DemandsList"
import Nodata from "@/components/List/Nodata"

const sendRequest = (type = 1) => {
  return axios.get("/api/trade/demand/getMyList", {
    params: {
      page: 1,
      page_size: 50,
      status: type
    }
  })
}

export default {
  name: "userSupplies",
  data() {
    return {
      tabs: ["求购中", "已停止", "被驳回"],
      active: 0,
      loading: false,
      saleList: [],
      unshelveList: [],
      refuseList: [],
      refuseDesc: []
    }
  },
  mounted() {
    this.getList()
  },
  methods: {
    changeActiveTab(index) {
      this.active = index
    },
    async getList() {
      this.loading = true
      let saleList = sendRequest(1)
      let unshelveList = sendRequest(2)
      let refuseList = sendRequest(3)
      Promise.all([saleList, unshelveList, refuseList])
        .then(v => {
          this.loading = false
          this.saleList = v[0].data.resp_data.data
          this.unshelveList = v[1].data.resp_data.data
          this.refuseList = v[2].data.resp_data.data
          this.refuseDesc = v[2].data.resp_data.data_reject
        })
        .catch(e => {
          this.loading = false
        })
    }
  },
  components: {
    Tab,
    TabItem,
    Swiper,
    SwiperItem,
    List,
    Loading,
    Nodata
  }
}
</script>

<style lang="scss">
@import "../../../styles/index.scss";

.mt-user-supplies {
  .mt-us-tips {
    line-height: 30px;
    padding-left: 10px;
  }

  .mt-us-content {
    position: fixed;
    top: 74px;
    left: 0px;
    right: 0px;
    bottom: 0px;
  }

  .tab-item {
    background: #fff;
    padding-bottom: 50px;
  }

  .vux-tab .vux-tab-item {
    font-size: 16px !important;
  }

  .vux-tab .vux-tab-item.vux-tab-selected {
    color: #008cf0 !important;
  }

  .vux-tab-ink-bar {
    background-color: #008cf0 !important;
  }

  .vux-swiper-item {
    padding-top: 10px;
    overflow-y: scroll;
  }
}
</style>
